package com.syh.jn.scm.infrastructure.repository.impl.driver.logistics;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.driver.entity.logistics.DriverLogisticsBill;
import com.syh.jn.scm.domain.driver.repository.logistics.DriverLogisticsBillRepository;
import com.syh.jn.scm.dto.driver.logistics.DriverLogisticsBillItemVo;
import com.syh.jn.scm.dto.driver.logistics.DriverLogisticsBillVo;
import com.syh.jn.scm.infrastructure.repository.model.logistics.LogisticsBillItemModel;
import com.syh.jn.scm.infrastructure.repository.model.logistics.LogisticsBillModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by soci.song on 2018/11/8.
 */
@Repository
public class DriverLogisticsBillRepositoryImpl extends AbstractAggregateRepository<DriverLogisticsBill,LogisticsBillModel,Long> implements DriverLogisticsBillRepository {

    @Override
    public List<DriverLogisticsBillItemVo> getItemsByBillId(Long id) {
        StringBuffer hql = new StringBuffer();
        hql.append("from LogisticsBillItemModel model where model.logisticsBillId = ?");
        List<LogisticsBillItemModel> itemModels = (List<LogisticsBillItemModel>) this.hibernateTemplate.find(hql.toString(),id);
        List<DriverLogisticsBillItemVo> items = new ArrayList<>();
        itemModels.forEach(itemModel -> {
            DriverLogisticsBillItemVo item = new DriverLogisticsBillItemVo();
            ExtBeanUtils.copyProperties(itemModel, item);
            items.add(item);
        });
        return items;
    }

    @Override
    public void updateStatus(DriverLogisticsBill logisticsBill) {
        super.update(logisticsBill);
    }

    @Override
    protected SQL getViewSql() {
        return new SQL(){{
            SELECT(SQLAliasGenerateUtil.aliasGenerate(LogisticsBillModel.class, "logisticsBill", null),
                    "logisticsCompany.NAME as logisticsCompanyName",
                    "storeHouse.NAME as depotName");
            FROM("T_LOGISTICS_BILL AS logisticsBill");
            LEFT_OUTER_JOIN(" T_BASE_LOGISTICS logisticsCompany ON logisticsBill.LOGISTICS_COMPANY_ID = logisticsCompany.ID");
            LEFT_OUTER_JOIN(" T_BASE_STOREHOUSE storeHouse ON logisticsBill.DEPOT_ID = storeHouse.ID");
        }};
    }

    @Override
    protected Class getViewObject() {
        return DriverLogisticsBillVo.class;
    }

    @Override
    protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
        Map<String, String> params = new HashMap<>();
        params.put("id", "logisticsBill.ID");
        params.put("driverContactPhone", "logisticsBill.DRIVER_CONTACT_PHONE");
        params.put("businessEntityId", "logisticsBill.BUSINESS_ENTITY_ID");
        params.put("logisticsState", "logisticsBill.LOGISTICS_STATE");
        params.put("subjectBillId", "logisticsBill.SUBJECT_BILL_ID");
        ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
            return params.get(propertyName);
        });
        return parse;
    }

}
